<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\model\Category;

/**
 * 关联测试
 *
 * @icon fa fa-circle-o
 */
class Guanlian extends Backend
{

    /**
     * Guanlian模型对象
     * @var \app\admin\model\Guanlian
     */
    protected $model = null;
    protected $dataLimitField = ['admin_id'];
    protected $relationSearch = true;
    protected $searchFields = ['name', 'admin.nickname'];
    protected $modelValidate = true;
    protected $modelSceneValidate = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Guanlian;
        $this->assignconfig('demo', ['name'=>'名称']);
    }


    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this
                ->buildparams();
            $list = $this->model->where($where)->with(['admin' => function($query) {
                $query->field('nickname,username,email');
            }])
                ->order($sort, $order)
                ->paginate(4);
            $dataList = $list->items();
            foreach ($dataList as $value) {
                if (isset($value['admin'])) {
                    unset($value['admin']);
                }
            }
            $result = array('total' => $list->total(), 'rows' => $list->items());
            return json($result);
        }

        return $this->view->fetch();
    }



}
